//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package database;

import java.util.Random;

import game.Obolo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
	private static final String DATABASE_NAME = "OinkObolo";
	private static final String TAG = "DBAdapter";
	private static final int DATABASE_VERSION = 1;

	/*---TABLE OBOLO---*/
	private static final String OBOLO_DATABASE_TABLE = "Obolo";
	public static final String OBOLO_PK = "idObolo";
	public static final String OBOLO_NORTH = "north";
	public static final String OBOLO_EAST = "east";
	public static final String OBOLO_SOUTH = "south";
	public static final String OBOLO_WEST = "west";
	private static final String OBOLO_DATABASE_CREATE = "create table Obolo "
			+ "(idObolo integer primary key autoincrement, "
			+ "north text not null, south text not null, "
			+ "east text not null, west text not null);";
	/*---END TABLE OBOLO---*/

	/*---TABLE PREFERENCE---*/
	private static final String PREFERENCE_DATABASE_TABLE = "Preference";
	public static final String PREFERENCE_PK = "idPreference";
	public static final String PREFERENCE_SIZE_BOARD = "sizeBoard";
	public static final String PREFERENCE_NUMBER_OF_PLAYERS = "numberOfPlayers";
	public static final String PREFERENCE_RIVAL_NAME = "rivalName";
	public static final String PREFERENCE_USER_NAME = "userName";
	public static final String PREFERENCE_DIFICULTY = "dificulty";
	private static final String PREFERENCE_DATABASE_CREATE = "create table Preference "
			+ "(idPreference integer primary key autoincrement, "
			+ "sizeBoard text not null, numberOfPlayers text not null, "
			+ "rivalName text not null, userName text not null, "
			+ "dificulty text not null);";
	/*---END TABLE PREFERENCE---*/

	/*---TABLE PLAYER---*/
	private static final String PLAYER_DATABASE_TABLE = "Player";
	public static final String PLAYER_PK = "idPlayer";
	public static final String PLAYER_LEVEL = "level";
	public static final String PLAYER_NAME = "name";
	public static final String PLAYER_GAMES_WIN = "gamesWin";
	public static final String PLAYER_GAMES_LOST = "gamesLost";
	public static final String PLAYER_HISTORY_STEP = "historyStep";
	public static final String PLAYER_FK_PREFERENCE = "idPreference";
	private static final String PLAYER_DATABASE_CREATE = "create table Player "
			+ "(idPlayer integer primary key autoincrement, "
			+ "level text not null, name text not null, gamesWin integer not null, "
			+ "gamesLost integer not null, historyStep integer not null, idPreference integer not null, "
			+ "foreign key(idPreference) references Preference(idPreference));";
	/*---END TABLE PLAYER---*/

	/*---TABLE RULE---*/
	private static final String RULE_DATABASE_TABLE = "Rule";
	public static final String RULE_PK = "idRule";
	public static final String RULE_NAME = "name";
	public static final String RULE_DESCRIPTION = "description";
	private static final String RULE_DATABASE_CREATE = "create table Rule "
			+ "(idRule integer primary key autoincrement, name text not null, "
			+ "description text not null);";
	/*---END TABLE RULE---*/

	/*---TABLE PLAYER_OBOLO---*/
	private static final String PLAYER_OBOLO_DATABASE_TABLE = "PlayerObolo";
	public static final String PLAYER_OBOLO_PK_FK_PLAYER = "idPlayer";
	public static final String PLAYER_OBOLO_PK_FK_OBOLO = "idObolo";
	public static final String PLAYER_OBOLO_QUANTITY = "quantity";
	private static final String PLAYER_OBOLO_DATABASE_CREATE = "create table PlayerObolo "
			+ "(idPlayer integer not null, idObolo integer not null, quantity integer not null, "
			+ "primary key(idObolo,idPlayer), foreign key(idObolo) references Obolo(idObolo), "
			+ "foreign key(idPlayer) references Player(idPlayer));";
	/*---END TABLE OBOLO_PLAYER---*/

	/*---TABLE PREFERENCE_RULE---*/
	private static final String PREFERENCE_RULE_DATABASE_TABLE = "PreferenceRule";
	public static final String PREFERENCE_RULE_PK_FK_PREFERENCE = "idPreference";
	public static final String PREFERENCE_RULE_PK_FK_RULE = "idRule";
	private static final String PREFERENCE_RULE_DATABASE_CREATE = "create table PreferenceRule "
			+ "(idPreference integer not null, idRule integer not null, "
			+ "primary key(idPreference, idRule), "
			+ "foreign key(idPreference) references Preference(idPreference) "
			+ "foreign key(idRule) references Rule(idRule));";
	/*---END TABLE PREFERENCE_RULE---*/

	private final Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public DBAdapter(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(OBOLO_DATABASE_CREATE);
			db.execSQL(PREFERENCE_DATABASE_CREATE);
			db.execSQL(PLAYER_DATABASE_CREATE);
			db.execSQL(RULE_DATABASE_CREATE);
			db.execSQL(PLAYER_OBOLO_DATABASE_CREATE);
			db.execSQL(PREFERENCE_RULE_DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			if (oldVersion < newVersion) {
				Log.w(TAG, "Actualizando la base de datos de la version "
						+ oldVersion + " a " + newVersion
						+ ", which will destroy all old data");
				db.execSQL("DROP TABLE IF EXISTS Obolo");
				db.execSQL("DROP TABLE IF EXISTS Preference");
				db.execSQL("DROP TABLE IF EXISTS Player");
				db.execSQL("DROP TABLE IF EXISTS Rule");
				db.execSQL("DROP TABLE IF EXISTS PlayerObolo");
				db.execSQL("DROP TABLE IF EXISTS PreferenceRule");
				onCreate(db);
			}

		}
	}

	// ---obre la base de dades---
	public DBAdapter open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}

	// ---tanca la base de dades---
	public void close() {
		db.close();
	}

	// ---comprova si la base de dades està creada o no---
	public boolean isCreated() {
		boolean answer = true;
		open();
		Cursor cObolo = getAllObolo();
		Cursor cPreference = getAllPreference();
		Cursor cRule = getAllRule();
		Cursor cPlayer = getAllPlayer();
		Cursor cPlayerObolo = getAllPlayerObolo();
		Cursor cPreferenceRule = getAllPreferenceRule();
		if (!cObolo.moveToFirst() && !cPreference.moveToFirst()
				&& !cRule.moveToFirst() && !cPlayer.moveToFirst()
				&& !cPlayerObolo.moveToFirst()
				&& !cPreferenceRule.moveToFirst()) {
			answer = false;
		}
		close();
		return answer;
	}

	// ---"reseteja" la base de dades---
	public void resetDB() {
		db = DBHelper.getReadableDatabase();
		db.execSQL("DROP TABLE IF EXISTS Obolo");
		db.execSQL("DROP TABLE IF EXISTS Preference");
		db.execSQL("DROP TABLE IF EXISTS Player");
		db.execSQL("DROP TABLE IF EXISTS Rule");
		db.execSQL("DROP TABLE IF EXISTS PlayerObolo");
		db.execSQL("DROP TABLE IF EXISTS PreferenceRule");
		db.execSQL(OBOLO_DATABASE_CREATE);
		db.execSQL(PREFERENCE_DATABASE_CREATE);
		db.execSQL(PLAYER_DATABASE_CREATE);
		db.execSQL(RULE_DATABASE_CREATE);
		db.execSQL(PLAYER_OBOLO_DATABASE_CREATE);
		db.execSQL(PREFERENCE_RULE_DATABASE_CREATE);
		createDB();
	}

	// Crea la nostra base de dades
	public void createDB() {
		createObolo(this);
		createPreference(this);
		createRule(this);
		createPlayer(this);
		createPlayerObolo(this);
		createPreferenceRule(this);
		Log.w(TAG, "Creando base de datos");
	}

	// Afegeix dades a la taula Obolo
	private void createObolo(DBAdapter dbAdapter) {
		dbAdapter.open();
		Cursor cObolo = dbAdapter.getAllObolo();
		if (!cObolo.moveToFirst()) {
			Random numRnd = new Random();
			String north, east, south, west, skill;
			;
			int side;

			for (int count = 0; count < 200; count++) {
				north = null;
				east = null;
				south = null;
				west = null;
				if (count % 10 == 0) {
					skill = selectSkill(numRnd.nextInt(3));
					side = numRnd.nextInt(4);
					switch (side) {
					case 0:
						north = skill;
						break;
					case 1:
						east = skill;
						break;
					case 2:
						south = skill;
						break;
					case 3:
						west = skill;
						break;
					}
				}
				if (north == null) {
					north = String.valueOf(numRnd.nextInt(9) + 1);
				}
				if (east == null) {
					east = String.valueOf(numRnd.nextInt(9) + 1);
				}
				if (south == null) {
					south = String.valueOf(numRnd.nextInt(9) + 1);
				}
				if (west == null) {
					west = String.valueOf(numRnd.nextInt(9) + 1);
				}
				dbAdapter.insertObolo(north, east, south, west);
			}
		}
		dbAdapter.close();
	}

	// Afegeix skills als obolos inserits
	private String selectSkill(int skillNumber) {
		String skillstr = null;
		switch (skillNumber) {
		case 0:
			skillstr = "sword";
			break;
		case 1:
			skillstr = "shield";
			break;
		case 2:
			skillstr = "twins";
			break;
		}
		return skillstr;
	}

	// Afegeix les preferencias o opcions a la taula Preference
	private void createPreference(DBAdapter dbAdapter) {
		dbAdapter.open();
		Cursor cPreference = dbAdapter.getAllPreference();
		if (!cPreference.moveToFirst()) {
			String sizeBoard, numberOfPlayers, rivalName, userName, dificulty;
			sizeBoard = "3x3";
			numberOfPlayers = "1";
			rivalName = "Android";
			userName = "Guest";
			dificulty = "Normal";
			dbAdapter.insertPreference(sizeBoard, numberOfPlayers, rivalName,
					userName, dificulty);
		}
		dbAdapter.close();
	}

	// Afegeix les regles a la taule Rules
	private void createRule(DBAdapter dbAdapter) {
		dbAdapter.open();
		Cursor cRule = dbAdapter.getAllRule();
		if (!cRule.moveToFirst()) {
			dbAdapter.insertRule("downward", "RuleDownWard");
			dbAdapter.insertRule("equal", "RuleEqual");
			dbAdapter.insertRule("equalChain", "RuleEqualChain");
			dbAdapter.insertRule("sum", "RuleSum");
			dbAdapter.insertRule("sumChain", "RuleSumChain");
			dbAdapter.insertRule("updownward", "RuleUpDownWard");
			dbAdapter.insertRule("upward", "RuleUpWard");
		}
		dbAdapter.close();
	}

	// Afegeix jugadors a la taula Player
	private void createPlayer(DBAdapter dbAdapter) {
		dbAdapter.open();
		Cursor cPlayer = dbAdapter.getAllPlayer();
		if (!cPlayer.moveToFirst()) {
			String name = dbAdapter.getPreference(1).getString(4);
			long gamesWin = 0;
			long gamesLost = 0;
			int historyStep = 0;
			long idPreference = dbAdapter.getPreference(1).getInt(0);
			dbAdapter.insertPlayer("0", name, gamesWin, gamesLost, historyStep,
					idPreference);
			dbAdapter.insertPlayer("1", "IANormal", gamesWin, gamesLost,
					historyStep, idPreference);
			dbAdapter.insertPlayer("2", "IAHard", gamesWin, gamesLost,
					historyStep, idPreference);
		}
		dbAdapter.close();
	}

	// Relaciona obolos amb un jugador mitjan�ant la taula PlayerObolo
	private void createPlayerObolo(DBAdapter dbAdapter) {
		dbAdapter.open();
		Cursor cPlayerObolo = dbAdapter.getAllPlayerObolo();
		// Si es pot moure al primer, vol dir que la taula te dades, sino esta
		// buida
		if (!cPlayerObolo.moveToFirst()) {
			Random varianRow = new Random();
			long quantity = 1;
			int maxObolos = dbAdapter.getAllObolo().getCount();

			// es recupera el idPlayer que sera sempre la fila 1 columna 0 de la
			// taula Player
			long idPlayer = dbAdapter.getPlayer(1).getInt(0);
			for (int row = 0; row < 20; row++) {
				// Recupera un idObolo a l'atzar
				long idObolo = dbAdapter.getObolo(
						row + varianRow.nextInt(maxObolos - row) + 1).getInt(0);
				// Mira si ja esta inserit
				Cursor cPlayerOboloNew = dbAdapter.getPlayerObolo(idPlayer,
						idObolo);
				// Si cPlayerObolo.moveToFirst() == true llavors aquest obolo ja
				// el té el jugador
				if (cPlayerOboloNew.moveToFirst()) {
					quantity = cPlayerOboloNew.getLong(cPlayerOboloNew
							.getColumnIndex("quantity"));
					quantity++;
					dbAdapter.updatePlayerObolo(idPlayer, idObolo, quantity);
				} else {
					dbAdapter.insertPlayerObolo(idPlayer, idObolo, quantity);
				}
				quantity = 1;
			}
		}
		dbAdapter.close();
	}

	// Relaciona les normas amb les preferencias dins de la taula PreferenceRule
	private void createPreferenceRule(DBAdapter dbAdapter) {
		dbAdapter.open();
		Cursor cPreferenceRule = dbAdapter.getAllPreferenceRule();
		if (!cPreferenceRule.moveToFirst()) {
			long idPreference = dbAdapter.getPlayer(1).getInt(6);
			Cursor cRule = dbAdapter.getAllRule();
			if (cRule.moveToFirst()) {
				dbAdapter.insertPreferenceRule(idPreference, cRule.getInt(0));
				while (cRule.moveToNext()) {
					dbAdapter.insertPreferenceRule(idPreference,
							cRule.getInt(0));
				}
			}
		}
		dbAdapter.close();
	}

	/*---FUNCTIONS OBOLO---*/
	// ---insereix un nou obolo---
	public long insertObolo(String north, String east, String south, String west) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(OBOLO_NORTH, north);
		initialValues.put(OBOLO_SOUTH, south);
		initialValues.put(OBOLO_EAST, east);
		initialValues.put(OBOLO_WEST, west);
		return db.insert(OBOLO_DATABASE_TABLE, null, initialValues);
	}

	// ---elimina un obolo en concret---
	public boolean deleteObolo(long idObolo) {
		return db.delete(OBOLO_DATABASE_TABLE, OBOLO_PK + "=" + idObolo, null) > 0;
	}

	// ---retorna tots els obolos---
	public Cursor getAllObolo() {
		return db.query(OBOLO_DATABASE_TABLE, new String[] { OBOLO_PK,
				OBOLO_NORTH, OBOLO_SOUTH, OBOLO_EAST, OBOLO_WEST }, null, null,
				null, null, null);
	}

	// ---retorna un obolo en concret---
	public Cursor getObolo(long idObolo) throws SQLException {
		Cursor mCursor = db.query(true, OBOLO_DATABASE_TABLE, new String[] {
				OBOLO_PK, OBOLO_NORTH, OBOLO_SOUTH, OBOLO_EAST, OBOLO_WEST },
				OBOLO_PK + "=" + idObolo, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor getObolo(Obolo obolo) throws SQLException {
		Cursor mCursor = db.query(
				true,
				OBOLO_DATABASE_TABLE,
				new String[] { OBOLO_PK, OBOLO_NORTH, OBOLO_SOUTH, OBOLO_EAST,
						OBOLO_WEST },
				OBOLO_NORTH + " LIKE '" + obolo.getNorthName() + "' AND "
						+ OBOLO_SOUTH + " LIKE '" + obolo.getSouthName()
						+ "' AND " + OBOLO_EAST + " LIKE '"
						+ obolo.getEastName() + "' AND " + OBOLO_WEST
						+ " LIKE '" + obolo.getWestName() + "'", null, null,
				null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	// ---actualitza un obolo en concret---
	public boolean updateObolo(long idObolo, String north, String east,
			String south, String west) {
		ContentValues args = new ContentValues();
		args.put(OBOLO_NORTH, north);
		args.put(OBOLO_SOUTH, south);
		args.put(OBOLO_EAST, east);
		args.put(OBOLO_WEST, west);
		return db.update(OBOLO_DATABASE_TABLE, args, OBOLO_PK + "=" + idObolo,
				null) > 0;
	}

	/*---END FUNCTIONS OBOLO---*/

	/*---FUNCTIONS PREFERENCE---*/
	public long insertPreference(String sizeBoard, String numberOfPlayers,
			String rivalName, String userName, String dificulty) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(PREFERENCE_SIZE_BOARD, sizeBoard);
		initialValues.put(PREFERENCE_NUMBER_OF_PLAYERS, numberOfPlayers);
		initialValues.put(PREFERENCE_RIVAL_NAME, rivalName);
		initialValues.put(PREFERENCE_USER_NAME, userName);
		initialValues.put(PREFERENCE_DIFICULTY, dificulty);
		return db.insert(PREFERENCE_DATABASE_TABLE, null, initialValues);
	}

	public boolean deletePreference(long idPreference) {
		return db.delete(PREFERENCE_DATABASE_TABLE, PREFERENCE_PK + "="
				+ idPreference, null) > 0;
	}

	public Cursor getAllPreference() {
		return db.query(PREFERENCE_DATABASE_TABLE, new String[] {
				PREFERENCE_PK, PREFERENCE_SIZE_BOARD,
				PREFERENCE_NUMBER_OF_PLAYERS, PREFERENCE_RIVAL_NAME,
				PREFERENCE_USER_NAME, PREFERENCE_DIFICULTY }, null, null, null,
				null, null);
	}

	public Cursor getPreference(long idPreference) throws SQLException {
		Cursor mCursor = db.query(true, PREFERENCE_DATABASE_TABLE,
				new String[] { PREFERENCE_PK, PREFERENCE_SIZE_BOARD,
						PREFERENCE_NUMBER_OF_PLAYERS, PREFERENCE_RIVAL_NAME,
						PREFERENCE_USER_NAME, PREFERENCE_DIFICULTY },
				PREFERENCE_PK + "=" + idPreference, null, null, null, null,
				null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public boolean updatePreference(long idPreference, String sizeBoard,
			String numberOfPlayers, String rivalName, String userName,
			String dificulty) {
		ContentValues args = new ContentValues();
		args.put(PREFERENCE_SIZE_BOARD, sizeBoard);
		args.put(PREFERENCE_NUMBER_OF_PLAYERS, numberOfPlayers);
		args.put(PREFERENCE_RIVAL_NAME, rivalName);
		args.put(PREFERENCE_USER_NAME, userName);
		args.put(PREFERENCE_DIFICULTY, dificulty);
		return db.update(PREFERENCE_DATABASE_TABLE, args, PREFERENCE_PK + "="
				+ idPreference, null) > 0;
	}

	/*---END FUNCTIONS PREFERENCE---*/

	/*---FUNCTIONS PLAYER---*/
	public long insertPlayer(String level, String name, long gamesWin,
			long gamesLost, int historyStep, long idPreference) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(PLAYER_LEVEL, level);
		initialValues.put(PLAYER_NAME, name);
		initialValues.put(PLAYER_GAMES_WIN, gamesWin);
		initialValues.put(PLAYER_GAMES_LOST, gamesLost);
		initialValues.put(PLAYER_HISTORY_STEP, historyStep);
		initialValues.put(PLAYER_FK_PREFERENCE, idPreference);
		return db.insert(PLAYER_DATABASE_TABLE, null, initialValues);
	}

	public boolean deletePlayer(long idPlayer) {
		return db.delete(PLAYER_DATABASE_TABLE, PLAYER_PK + "=" + idPlayer,
				null) > 0;
	}

	public Cursor getAllPlayer() {
		return db.query(PLAYER_DATABASE_TABLE, new String[] { PLAYER_PK,
				PLAYER_LEVEL, PLAYER_NAME, PLAYER_GAMES_WIN, PLAYER_GAMES_LOST,
				PLAYER_HISTORY_STEP, PLAYER_FK_PREFERENCE }, null, null, null,
				null, null);
	}

	public Cursor getPlayer(long idPlayer) throws SQLException {
		Cursor mCursor = db.query(true, PLAYER_DATABASE_TABLE, new String[] {
				PLAYER_PK, PLAYER_LEVEL, PLAYER_NAME, PLAYER_GAMES_WIN,
				PLAYER_GAMES_LOST, PLAYER_HISTORY_STEP, PLAYER_FK_PREFERENCE },
				PLAYER_PK + "=" + idPlayer, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public boolean updatePlayer(long idPlayer, String level, String name,
			long gamesWin, long gamesLost, int historyStep, long idPreference) {
		ContentValues args = new ContentValues();
		args.put(PLAYER_LEVEL, level);
		args.put(PLAYER_NAME, name);
		args.put(PLAYER_GAMES_WIN, gamesWin);
		args.put(PLAYER_GAMES_LOST, gamesLost);
		args.put(PLAYER_HISTORY_STEP, historyStep);
		args.put(PLAYER_FK_PREFERENCE, idPreference);
		return db.update(PLAYER_DATABASE_TABLE, args, PLAYER_PK + "="
				+ idPlayer, null) > 0;
	}

	/*---END FUNCTIONS PLAYER---*/

	/*---FUNCTIONS RULE---*/
	public long insertRule(String name, String description) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(RULE_NAME, name);
		initialValues.put(RULE_DESCRIPTION, description);
		return db.insert(RULE_DATABASE_TABLE, null, initialValues);
	}

	public boolean deleteRule(long idRule) {
		return db.delete(RULE_DATABASE_TABLE, RULE_PK + "=" + idRule, null) > 0;
	}

	public Cursor getAllRule() {
		return db.query(RULE_DATABASE_TABLE, new String[] { RULE_PK, RULE_NAME,
				RULE_DESCRIPTION }, null, null, null, null, null);
	}

	public Cursor getRule(long idRule) throws SQLException {
		Cursor mCursor = db.query(true, RULE_DATABASE_TABLE, new String[] {
				RULE_PK, RULE_NAME, RULE_DESCRIPTION }, RULE_PK + "=" + idRule,
				null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public boolean updateRule(long idRule, String name, String description) {
		ContentValues args = new ContentValues();
		args.put(RULE_NAME, name);
		args.put(RULE_DESCRIPTION, description);
		return db.update(RULE_DATABASE_TABLE, args, RULE_PK + "=" + idRule,
				null) > 0;
	}

	/*---END FUNCTIONS RULE---*/

	/*---FUNCTIONS PLAYER_OBOLO---*/
	public long insertPlayerObolo(long idPlayer, long idObolo, long quantity) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(PLAYER_OBOLO_PK_FK_PLAYER, idPlayer);
		initialValues.put(PLAYER_OBOLO_PK_FK_OBOLO, idObolo);
		initialValues.put(PLAYER_OBOLO_QUANTITY, quantity);
		return db.insert(PLAYER_OBOLO_DATABASE_TABLE, null, initialValues);
	}

	public boolean deletePlayerObolo(long idPlayer, long idObolo) {
		return db.delete(PLAYER_OBOLO_DATABASE_TABLE, PLAYER_OBOLO_PK_FK_PLAYER
				+ "=" + idPlayer + " AND " + PLAYER_OBOLO_PK_FK_OBOLO + "="
				+ idObolo, null) > 0;
	}

	public Cursor getAllPlayerObolo() {
		return db.query(PLAYER_OBOLO_DATABASE_TABLE, new String[] {
				PLAYER_OBOLO_PK_FK_PLAYER, PLAYER_OBOLO_PK_FK_OBOLO,
				PLAYER_OBOLO_QUANTITY }, null, null, null, null, null);
	}

	public Cursor getPlayerObolo(long idPlayer, long idObolo)
			throws SQLException {
		Cursor mCursor = db.query(true, PLAYER_OBOLO_DATABASE_TABLE,
				new String[] { PLAYER_OBOLO_PK_FK_PLAYER,
						PLAYER_OBOLO_PK_FK_OBOLO, PLAYER_OBOLO_QUANTITY },
				PLAYER_OBOLO_PK_FK_PLAYER + "=" + idPlayer + " AND "
						+ PLAYER_OBOLO_PK_FK_OBOLO + "=" + idObolo, null, null,
				null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public boolean updatePlayerObolo(long idPlayer, long idObolo, long quantity) {
		ContentValues args = new ContentValues();
		args.put(PLAYER_OBOLO_PK_FK_PLAYER, idPlayer);
		args.put(PLAYER_OBOLO_PK_FK_OBOLO, idObolo);
		args.put(PLAYER_OBOLO_QUANTITY, quantity);
		return db.update(PLAYER_OBOLO_DATABASE_TABLE, args,
				PLAYER_OBOLO_PK_FK_PLAYER + "=" + idPlayer + " AND "
						+ PLAYER_OBOLO_PK_FK_OBOLO + "=" + idObolo, null) > 0;
	}

	/*---END FUNCTIONS PLAYER_OBOLO---*/

	/*---FUNCTIONS PREFERENCE_RULE---*/
	public long insertPreferenceRule(long idPreference, long idRule) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(PREFERENCE_RULE_PK_FK_PREFERENCE, idPreference);
		initialValues.put(PREFERENCE_RULE_PK_FK_RULE, idRule);
		return db.insert(PREFERENCE_RULE_DATABASE_TABLE, null, initialValues);
	}

	public boolean deletePreferenceRule(long idPreference, long idRule) {
		return db.delete(PREFERENCE_RULE_DATABASE_TABLE,
				PREFERENCE_RULE_PK_FK_PREFERENCE + "=" + idPreference + " AND "
						+ PREFERENCE_RULE_PK_FK_RULE + "=" + idRule, null) > 0;
	}

	public Cursor getAllPreferenceRule() {
		return db.query(PREFERENCE_RULE_DATABASE_TABLE, new String[] {
				PREFERENCE_RULE_PK_FK_PREFERENCE, PREFERENCE_RULE_PK_FK_RULE },
				null, null, null, null, null);
	}

	public Cursor getPreferenceRule(long idPreference, long idRule)
			throws SQLException {
		Cursor mCursor = db.query(true, PREFERENCE_RULE_DATABASE_TABLE,
				new String[] { PREFERENCE_RULE_PK_FK_PREFERENCE,
						PREFERENCE_RULE_PK_FK_RULE },
				PREFERENCE_RULE_PK_FK_PREFERENCE + "=" + idPreference + " AND "
						+ PREFERENCE_RULE_PK_FK_RULE + "=" + idRule, null,
				null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public boolean updatePreferenceRule(long idPreference, long idRule) {
		ContentValues args = new ContentValues();
		args.put(PREFERENCE_RULE_PK_FK_PREFERENCE, idPreference);
		args.put(PREFERENCE_RULE_PK_FK_RULE, idRule);
		return db.update(PREFERENCE_RULE_DATABASE_TABLE, args,
				PREFERENCE_RULE_PK_FK_PREFERENCE + "=" + idPreference + " AND "
						+ PREFERENCE_RULE_PK_FK_RULE + "=" + idRule, null) > 0;
	}
	/*---END FUNCTIONS PREFERENCE_RULE---*/
}